package com.example.project.bootstrap.extend;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.transaction.config.TransactionManagementConfigUtils;

//AutoProxyRegistrar
public class MyImportBDRegistrar implements ImportBeanDefinitionRegistrar {

// 所有有Import注解的类，都会被此方法拦截importingClassMetadata
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);

		System.out.println("registerBeanDefinitions " + importingClassMetadata.getClassName()
				+ " isAnnotated by EnableResourcelessTransactionManager ="
				+ importingClassMetadata.isAnnotated(EnableTransactionManagerImportAware.class.getName()));
		
//		importingClassMetadata.isAnnotated("EnableResourcelessTransactionManager");
		if(importingClassMetadata.isAnnotated(EnableTransactionManagerImportAware.class.getName()))
		{
			try {
				String transactionManagerBeanName = TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME;
				if(!registry.isBeanNameInUse(transactionManagerBeanName))
				{
					transactionManagerBeanName = TransactionManagementConfigUtils.TRANSACTION_ASPECT_BEAN_NAME;
					if(!registry.isBeanNameInUse(transactionManagerBeanName))
					{
						System.out.println("--------------------- 无法查找到 TransactionManager ------------------------");
						
						return;
					}
					System.out.println("--------------------- 当前 TransactionManager 代理模式 "+transactionManagerBeanName+" ------------------------");
				}
				
			} catch (NoSuchBeanDefinitionException e) {
				e.printStackTrace();
			}
		}
	}

}
